package com.biglead.orderservice.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.biglead.common.config.Constant;
import com.biglead.common.pojo.GoodsInfo;
import com.biglead.orderservice.mapper.GoodsInfoMapper;
import com.biglead.orderservice.service.GoodsInfoService;
import com.biglead.orderservice.service.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class GoodsInfoServiceImple extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements GoodsInfoService {

    @Resource
    GoodsInfoMapper goodsInfoMapper;
    @Resource
    RedisService redisService;

    /**
     * 测试使用将商品数据加载到redis中
     *
     * @param sid
     */
    @Override
    public void testRedisLoadGoods(Long sid) {
        GoodsInfo goodsInfo = this.getById(sid);
        if (goodsInfo == null) {
            return;
        }
        Long goodsId = goodsInfo.getId();
        redisService.set(Constant.GOOOD_STOCK + goodsId, goodsInfo.getGoodsStock());
        redisService.set(Constant.GOOOD_SALE + goodsId, goodsInfo.getGoodsSale());
        redisService.set(Constant.GOOOD_VERSION + goodsId, goodsInfo.getVersion());
        redisService.set(Constant.GOODS_NAME + goodsId, goodsInfo.getGoodsName());
        redisService.set(Constant.GOODS_INFO + goodsId, goodsInfo);
    }

    @Override
    public GoodsInfo getGoodsInfoById(Long sid) {
        return this.getById(sid);
    }

    @Override
    public boolean updateStockById(GoodsInfo goodsInfo) {
        return goodsInfoMapper.updateStockById(goodsInfo);
    }

    @Override
    public boolean updateStockByOptimistic(GoodsInfo goodsInfo) {
        return goodsInfoMapper.updateStockByOptimistic(goodsInfo);
    }
}
